package com.example.chenduoyun.shuffle.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

import com.example.chenduoyun.shuffle.util.Utils;

public class SlideMenu extends ViewGroup {

    private int downX;
    private int downY;
    private int moveX;
    private int currentState = MAIN_STATE;
    public static final int MAIN_STATE = 0;
    public static final int MENU_STATE = 1;
    private Scroller scroller;

    public SlideMenu(Context context) {
        super(context);
        init();
    }

    public SlideMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SlideMenu(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        scroller = new Scroller(getContext());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        View leftMenu = getChildAt(0);
        leftMenu.measure(leftMenu.getLayoutParams().width, heightMeasureSpec);

        View mainContent = getChildAt(1);
        mainContent.measure(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View leftMenu = getChildAt(0);
        leftMenu.layout(-leftMenu.getMeasuredWidth(), 0, 0, b);
        Utils.LogI("cdy", "onLayout: leftMenu.getMeasuredWidth()+" + leftMenu.getMeasuredWidth());
        getChildAt(1).layout(l, t, r, b);
        Utils.LogI("cdy", "onLayout: lw" + l + "   t" + t + "   r" + r + "    b" + b);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) event.getX();
                break;

            case MotionEvent.ACTION_MOVE:
                moveX = (int) event.getX();
                int scrollX = downX - moveX;

                int newScrollPostion = getScrollX() + scrollX;

                if (newScrollPostion < -getChildAt(0).getMeasuredWidth()) {
                    scrollTo(-getChildAt(0).getMeasuredWidth(), 0);
                } else if (newScrollPostion > 0) {
                    scrollTo(0, 0);
                } else {
                    scrollBy(scrollX, 0);
                }
                downX = moveX;
                break;

            case MotionEvent.ACTION_UP:
                int leftCenter = (int) (-getChildAt(0).getMeasuredWidth() / 2.0f);
                Utils.LogI("cdy", "onTouchEvent: leftContenr  +" + leftCenter);
                Utils.LogI("cdy", "onTouchEvent: getScrollX()  +" + getScrollX());
                if (getScrollX() < leftCenter) {
                    currentState = MENU_STATE;
                    updateCurrentContent();
                } else {
                    currentState = MAIN_STATE;
                    updateCurrentContent();
                }

                break;
            default:
                break;

        }
        return true;
    }

    private void updateCurrentContent() {
        int startX = getScrollX();
        int dx = 0;
        if (currentState == MENU_STATE) {
            dx = -getChildAt(0).getMeasuredWidth() - startX;
        } else {
            dx = 0 - startX;
        }
        int duration = Math.abs(dx * 2);
        scroller.startScroll(startX, 0, dx, 0, duration);
        invalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            int currX = scroller.getCurrX();
            scrollTo(currX, 0);
            invalidate();
        }
    }

    public void open() {
        currentState = MENU_STATE;
        updateCurrentContent();
    }

    public void close() {
        currentState = MAIN_STATE;
        updateCurrentContent();
    }

    public void switchState() {
        if (currentState == MAIN_STATE) {
            open();
        } else {
            close();
        }
    }

    public int getCurrentState() {
        return currentState;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) ev.getX();
                downY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float xOffset = Math.abs(ev.getX() - downX);
                float yOffset = Math.abs(ev.getY() - downY);
                if (xOffset > yOffset && xOffset > 5) {
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}
